package com.javasm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.AftersaleOutCriteria;
import com.javasm.domin.entity.AftersaleOut;
import com.javasm.domin.vo.AftersaleOutVo;
import com.javasm.mapper.AftersaleOutMapper;
import com.javasm.service.AftersaleOutService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.AftersaleOutTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class AftersaleOutServiceImpl extends BaseServiceimpl<AftersaleOut> implements AftersaleOutService {

    private final AftersaleOutMapper aftersaleOutMapper;
    private final AftersaleOutTransfer aftersaleOutTransfer;

    @Override
    public PageResult<AftersaleOutVo> searchPage(AftersaleOutCriteria aftersaleOutCriteria) {
        //开启分页
        PageHelper.startPage(aftersaleOutCriteria.getCurrentPage(),aftersaleOutCriteria.getPageSize());
        //条件创建
        LambdaQueryWrapper<AftersaleOut> lambda = new QueryWrapper<AftersaleOut>().lambda();
        if(aftersaleOutCriteria.getEstablishId()!=null&&aftersaleOutCriteria.getEstablishId()!=""){
            lambda.like(AftersaleOut::getEstablishId, aftersaleOutCriteria.getEstablishId());
        }
        if(!StringUtils.isEmpty(aftersaleOutCriteria.getEstablishstartTime())){
            lambda.between(AftersaleOut::getEstablishTime, aftersaleOutCriteria.getEstablishstartTime(), aftersaleOutCriteria.getEstablishendTime());
        }

        if(aftersaleOutCriteria.getOutSignId()!=null&&aftersaleOutCriteria.getOutSignId()!=0){
            lambda.eq(AftersaleOut::getOutSignId, aftersaleOutCriteria.getOutSignId());
        }
        if(aftersaleOutCriteria.getNumber()!=null&&aftersaleOutCriteria.getNumber()!=0){
            lambda.eq(AftersaleOut::getNumber, aftersaleOutCriteria.getNumber());
        }
        if(aftersaleOutCriteria.getBatchState()!=null&&aftersaleOutCriteria.getBatchState()!=0){
            lambda.eq(AftersaleOut::getBatchState, aftersaleOutCriteria.getBatchState());
        }
        if(!StringUtils.isEmpty(aftersaleOutCriteria.getStartTime())){
            lambda.between(AftersaleOut::getConfirmTime, aftersaleOutCriteria.getStartTime(), aftersaleOutCriteria.getEndTime());
        }

        List<AftersaleOut> aftersaleOuts = aftersaleOutMapper.selectList(lambda);
        /*获取总条数*/
        PageInfo<AftersaleOut> aftersaleOutPageInfo = new PageInfo<>(aftersaleOuts);
        return new PageResult<AftersaleOutVo>(aftersaleOutPageInfo.getTotal(),aftersaleOutTransfer.jotoVo(aftersaleOuts));


    }
}
